package Http;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Aservlet
 */
@WebServlet("/Aservlet")
public class Aservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Aservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Cookie[] kie=request.getCookies();
		String name =null;
	String pass =null;
	if(kie!=null) {
		for(Cookie cookie:kie) {
			if(cookie.getName().equals("username")) {
				name=cookie.getValue();
			}
			if(cookie.getName().equals("password")) {
				pass=cookie.getValue();
			}
	}
		PrintWriter pw=response.getWriter();
		pw.write("<!DOCTYPE html>\r\n" + 
				"<html>\r\n" + 
				"<head>\r\n" + 
				"<meta charset=\"UTF-8\">\r\n" + 
				"<title>Insert title here</title>\r\n" + 
				"</head>\r\n" + 
				"<body>\r\n" + 
				"<form action=\"CookServlet\">\r\n" + 
				"用户名<input type=\"text\" name=\"username\" value="+name+"><br>\r\n" + 
				"密码<input type=\"password\" name=\"password\" value="+pass+"><br>\r\n" + 
				"自动登录<input type=\"checkbox\" name=\"chex\" value=\"选择\" checked><br>\r\n" + 
				"<input type=\"submit\" name=\"Submit\" value=\"登录\"><br>\r\n" + 
				"</form>\r\n" + 
				"</body>\r\n" + 
				"</html>");

	}else {
		response.sendRedirect("work3.html");
	}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
